﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using API.Models;
using PPG.DataAccess;
using PPG.CryptoProvider;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;

namespace API.Controllers
{
    public class AdvancedSearchController : ApiController
    {
        DataSourceCommunicator dsComm = new DataSourceCommunicator(DataSourceCommunicator.ParamType.ServerCredentials, ConfigurationManager.AppSettings["DBServerName"].ToString(), ConfigurationManager.AppSettings["DBUserName"].ToString(), ConfigurationManager.AppSettings["DBPassword"].ToString());

        public HttpResponseMessage OptionsPatientDiagnosis()
        {
            var response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            return response;
        }

        public AdvancedSearchResponse AdvancedSearch([FromBody]AdvancedSearchRequest request)
        {
            AdvancedSearchResponse response = new AdvancedSearchResponse();
            try
            { 
                dsComm.AddParameter("@ParamLoginID", request.LoginID);
                dsComm.AddParameter("@ParamFromDate", request.FromDate);
                dsComm.AddParameter("@ParamToDate", request.ToDate);
                dsComm.AddParameter("@ParamReferralType", request.ReferralType);
                dsComm.AddParameter("@ParamReferralStatus", request.ReferralStatus);
                if(request.FromPractice == "All")
                {
                    dsComm.AddParameter("@ParamFromPractice", -1);
                }
                else
                {
                    dsComm.AddParameter("@ParamFromPractice", request.FromPractice);
                }
                if (request.ToPractice == "All")
                {
                    dsComm.AddParameter("@ParamToPractice", -1);
                }
                else
                {
                    dsComm.AddParameter("@ParamToPractice", request.ToPractice);
                }
                if (request.FromPhysician == "All")
                {
                    dsComm.AddParameter("@ParamFromPhysician", -1);
                }
                else
                {
                    dsComm.AddParameter("@ParamFromPhysician", request.FromPhysician);
                }
                if (request.ToPhysician == "All")
                {
                    dsComm.AddParameter("@ParamToPhysician", -1);
                }
                else
                {
                    dsComm.AddParameter("@ParamToPhysician", request.ToPhysician);
                }

                string data = dsComm.ExecuteStoredProcedure("dbo.spGetAdvancedSearchResultsForUser");
                XmlTextReader xReader = new XmlTextReader(data, XmlNodeType.Document, null);
                DataSet SearchResultDataSet = new DataSet();
                SearchResultDataSet.ReadXml(xReader);

                if(SearchResultDataSet.Tables.Count > 0)
                {
                    response.SearchResponse = SearchResultDataSet.Tables[0];
                }
                else
                {
                    response.ErrorMessage = "No Data";
                }
            }
            catch(Exception ex)
            {
                response.ErrorMessage = ex.Message;
            }
            return response;
        }

    }
}
